home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bc_ti.zip / TI400.ASC < prev    next >
Text File  |  1992-02-25  |  5KB  |  265 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  TURBO C/TURBO PROLOG                   NUMBER  :  400
  9.   VERSION  :  1.0/1.1
  10.        OS  :  PC-DOS
  11.      DATE  :  January 6, 1988                          PAGE  :  1/4
  12.  
  13.     TITLE  :  SIMULATING NON-DETERMINISTIC CALLS IN C
  14.  
  15.  
  16.  
  17.  
  18.   When writing Turbo C functions to interface with Turbo Prolog, it
  19.   is often desirable to have functions which behave non-
  20.   deterministically.  Following is an example program which shows
  21.   how to write C functions which simulate non-deterministic Prolog
  22.   predicates.
  23.  
  24.   Type the Turbo Prolog program and compile the code to an .OBJ
  25.   file.  After this is done, type the Turbo C program and compile
  26.   it to an .OBJ file as well.  Use the following command from the
  27.   DOS prompt to compile the C module:
  28.  
  29.   TCC -u- -r- -ml -c -O NONDETC.C
  30.  
  31.   Once both source code files are successfully compiled into object
  32.   code, issue the following link command from the DOS command line
  33.   to create the executable file NONDET.EXE.
  34.  
  35.   TLINK INIT+CPINIT+NONDET+NONDETC+NONDET.SYM,NONDET,,PROLOG
  36.  
  37.   Here is the Turbo Prolog source code:
  38.  
  39.   /*--------------------------------------------------------------
  40.    * NONDET.PRO -- Prolog program to demonstrate non-deterministic
  41.    *               calls of C functions.
  42.    *--------------------------------------------------------------
  43.    */
  44.  
  45.   global predicates
  46.    c_range(integer,integer,integer,integer) - (i,i,o,i) language c
  47.    cpinit                                   -           language c
  48.    backtrack(integer)                       - (i)       language c
  49.    genenv(integer)                          - (o)       language c
  50.  
  51.   predicates
  52.     continue(integer)
  53.     range(integer,integer,integer).
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  TURBO C/TURBO PROLOG                   NUMBER  :  400
  75.   VERSION  :  1.0/1.1
  76.        OS  :  PC-DOS
  77.      DATE  :  January 6, 1988                          PAGE  :  2/4
  78.  
  79.     TITLE  :  SIMULATING NON-DETERMINISTIC CALLS IN C
  80.  
  81.  
  82.  
  83.  
  84.   clauses
  85.  
  86.   /* Succeeds if the C call with environment Env can be re-
  87.    * satisfied.
  88.    */
  89.     continue(_).
  90.     continue(Env) :- backtrack(Env),
  91.                      continue(Env).
  92.  
  93.   /* This predicate is equivilent to the predicate:
  94.    *    range(Start,End,Start) :- Start <= End.
  95.    *    range(Start,End,X)     :- Start < End, Y = Start + 1,
  96.    *                              range(Y,End,X).*/
  97.     range(Start,End,X) :- genenv(Env),
  98.                           continue(Env),
  99.                           c_range(Start,End,X,Env).
  100.  
  101.  
  102.   /* Display the numbers from 1 to 100.
  103.    */
  104.   goal cpinit,
  105.        clearwindow,
  106.        range(1,100,X),
  107.        write(X),
  108.        readchar(_),
  109.        nl,
  110.        fail.
  111.  
  112.  
  113.   Following is the Turbo C source code:
  114.  
  115.   /* Implements a C function equivilent to the Prolog predicate:
  116.    *    range(Start,End,Start) :- Start <= End.
  117.    *    range(Start,End,X)     :- Start < End, Y = Start + 1,
  118.     *                              range(Y,End,X)..*/
  119.  
  120.   static int env = 0;
  121.   void fail_cc();
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  TURBO C/TURBO PROLOG                   NUMBER  :  400
  141.   VERSION  :  1.0/1.1
  142.        OS  :  PC-DOS
  143.      DATE  :  January 6, 1988                          PAGE  :  3/4
  144.  
  145.     TITLE  :  SIMULATING NON-DETERMINISTIC CALLS IN C
  146.  
  147.  
  148.  
  149.  
  150.   #define MAX_ENV 128
  151.  
  152.   static int environ[MAX_ENV];
  153.  
  154.   /* Returns the index to an unused environment cell */
  155.   void genenv_0(int *x)
  156.  
  157.   {
  158.      int i;
  159.  
  160.      /* If a cell is 0, it is unused */
  161.      for (i=0; i<MAX_ENV && environ[i]; i++)
  162.        ;
  163.  
  164.      if (i<MAX_ENV)
  165.        *x = i;
  166.      else /* all are in use */
  167.        fail_cc();
  168.   }
  169.  
  170.   /* Fail if c_range can nolonger be satisfied */
  171.   void backtrack_0(int env)
  172.   {
  173.     if ( !environ[env] )
  174.       fail_cc();
  175.   }
  176.  
  177.   void c_range_0(int begin, int end, int *x , int env)
  178.   {
  179.     /* If the environment is 0, this is the initial call
  180.      * to c_range.  Tell backtrack() that if all else fails,
  181.      * there is at least one more number to try.  This is
  182.      * done by initiating the environment cell to non-zero.
  183.      */
  184.     if ( !environ[env] )
  185.  
  186.       *x = environ[env] = begin;
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  TURBO C/TURBO PROLOG                   NUMBER  :  400
  207.   VERSION  :  1.0/1.1
  208.        OS  :  PC-DOS
  209.      DATE  :  January 6, 1988                          PAGE  :  4/4
  210.  
  211.     TITLE  :  SIMULATING NON-DETERMINISTIC CALLS IN C
  212.  
  213.  
  214.  
  215.  
  216.     /* Upon backtracking,return the next highest value
  217.        within range       */
  218.  
  219.     else if (environ[env] < end)
  220.      *x = ++environ[env];
  221.     /* All values have been tried, cause the predicate to fail */
  222.     else
  223.     {
  224.       /* Tell backtrack() to fail */
  225.       environ[env] = 0;
  226.       /* Tell prolog I failed */
  227.       fail_cc();
  228.     }
  229.   }
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.